﻿﻿<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <%@ include file="../common/header.jsp"%>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <%@ include file="../common/top.jsp"%>

    <div class="row">
        <div class="col-sm-3">
            <!-- <#assign menu="systemDictionaryItem"%> -->
            <%@ include file="../common/menu.jsp"%>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">字典明细管理</h1>
                </div>
            </div>
            <!-- 高级查询 -->
            <form class="form-inline" id="searchForm" action="/systemDictionaryItem/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <input type="hidden" name="parentId" value="${qo.parentId}">
            </form>

            <div class="row">
                <div class="col-sm-4">
                    <!-- 字典目录 -->
                    <ul id="menu" class="list-group">
                        <li class="list-group-item">
                            <a role="button">
                                <span>字典目录</span>
                            </a>
                            <ul class="in">
                                <!-- 循环迭代显示字典目录 -->
                               <!--  <#list dictionaries as d> -->
                                 <c:forEach var="d" items="${dictionaries}">
                                    <li data-did="${d.id}">
                                        <a role="button">${d.title}</a>
                                    </li>
                                  </c:forEach>
                                <!-- </#list> -->
                            </ul>
                        </li>
                    </ul>
                </div>
                <div class="col-sm-8">
                    <!-- <#if pageInfo??> -->
                    <c:if test="${not empty pageInfo}" >
                        <!-- 字典明细 -->
                        <a role="button" class="btn btn-success inputBtn">
                            <span class="glyphicon glyphicon-plus"></span> 添加明细
                        </a>
                        <table class="table table-striped table-hover">
                            <tr>
                                <th>编号</th>
                                <th>明细标题</th>
                                <th>明细序列</th>
                                <th>操作</th>
                            </tr>
                            <!-- <#list pageInfo.list as entity> -->
                             <c:forEach var="entity" items="${pageInfo.list}" varStatus="status">
                                <tr>
                                    <td>${status.index + 1}</td>
                                    <td>${entity.title}</td>
                                    <td>${entity.sequence}</td>
                                    <td>
                                        <a role="button" class="btn btn-info btn-xs inputBtn" data-json='${entity.json}'>
                                            <span class="glyphicon glyphicon-pencil"></span> 编辑
                                        </a>
                                        <a role="button" data-url="/systemDictionaryItem/delete.do?id=${entity.id}"
                                            class="btn btn-danger btn-xs deleteBtn">
                                            <span class="glyphicon glyphicon-trash"></span> 删除
                                        </a>
                                    </td>
                                </tr>
                                </c:forEach>
                            <!--  </#list> -->
                        </table>

                        <!--分页-->
                        <%@ include file="../common/page.jsp" %>
                    <!-- </#if> -->
                    </c:if>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 字典明细编辑的模态框 -->
<div class="modal fade" id="systemDictionaryModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <!--模态框的头部-->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="systemDictionaryModal">编辑</h4>
            </div>
            <!--模态框的正文-->
            <div class="modal-body">
                <form class="form-horizontal" action="/systemDictionaryItem/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <input type="hidden" name="parentId">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">字典目录：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="parent" readonly>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-4 control-label">明细标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="title" placeholder="请输入明细标题">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">明细序列：</label>
                        <div class="col-sm-6">
                            <input type="number" class="form-control" name="sequence" placeholder="请输入明细序列">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary systemDictionarySubmitBtn">保存</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        var dictionary; //当前选中字典

        //点击字典目录,发送当前字典的id去查询对应的明细
        $("li[data-did]").click(function () {
            var parentId = $(this).data("did");
            //给表单设置parentId
            $("#searchForm input[name='parentId']").val(parentId);
            //提交表单
            $("#searchForm").submit();
        });

        //回显当前的明细
        var selected = $("li[data-did='${qo.parentId}']").addClass("active");
        //保存当前选中的字典
        dictionary = {id: selected.data("did"), title: selected.find("a").html()};

        //找到所有的inputBtn类绑定点击事件
        $(".inputBtn").click(function () {
            //1:先清空模态框数据,再回显当前的目录
            $("#editForm input").val("");
            $("#editForm input[name='parentId']").val(dictionary.id); //回显字典id,隐藏
            $("#parent").val(dictionary.title); //回显字典目录

            //2:尝试拿到当前按钮上的JSON数据
            //拿到到data-json
            var json = $(this).data("json"); //去当前对象上的自定义属性的数据
            //3:只有编辑按钮才要回显数据
            if (json) {
                //把数据回显到模态框中
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='title']").val(json.title);
                $("#editForm input[name='sequence']").val(json.sequence);
            }
            //4:弹出模态框
            $("#systemDictionaryModal").modal("show");
        });

        //异步提交表单
        $("#editForm").ajaxForm(function (data) {
            doSuccess();
        });

        //给表单按钮绑定点击事件,提交表单
        $(".systemDictionarySubmitBtn").click(function () {
            $("#editForm").submit();
        });
    });
</script>
</body>
</html>